﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace SharpSoft.Data.DAOS
{
    public class Config_DAO : DAO
    {
        public Config_DAO(Database p_db, int p_userid = -1) : base(p_db, p_userid)
        {
        }

        protected override string InitScriptFileName => "config.txt";

        public string GetValue(string key)
        {
            var sql = "SELECT `cfgValue` FROM `Sys_Configs` WHERE `cfgKey`=@key;";
            return this.db.ExecuteString(sql, new { key });
        }
        public void SetValue(string key, string value)
        {
            var sql = "REPLACE INTO `Sys_Configs`(`cfgKey`,`cfgValue`)VALUES(@key,@value);";
            this.db.ExecuteString(sql, new { key, value });
        }

        public Dictionary<string, string> GetAll()
        {

            var sql = "SELECT * FROM `Sys_Configs`;";
            var dt = db.ExecuteDataTable(sql);
            var dic = new Dictionary<string, string>();
            foreach (DataRow item in dt.Rows)
            {
                dic.Add((string)item["cfgKey"], (string)item["cfgValue"]);
            }
            return dic;
        }
    }
}
